const express = require('express')
const router = express.Router()
const farmlandDao = require('../database/farmlandDao')
router.get('/getFarmlandByID',async (req,res) => {
  let id = req.query.farmlandId
  try{
    let result = await farmlandDao.getFarmlandByID(id)
    res.send({
      code:200,
      msg:'获取成功',
      data:result
    })
  }catch(err){
    res.send({
      code:400,
      msg:'获取失败',
      data:err
    })
  }
})
router.get('/getAllFarmland',async (req,res) => {
  try{
    let result = await farmlandDao.getAllFarmland()
    res.send({
      code:200,
      msg:'获取成功',
      data:result
    })
  }catch(err){
    res.send({
      code:400,
      msg:'获取失败',
      data:err
    })
  }
})
router.post('/addFarmland',async (req,res) => {
  try{
    let { name,type,manager_id }  = req.body
    let result = await farmlandDao.addFarmland(name,type,manager_id)
    res.send({
      code:200,
      msg:'获取成功',
      data:result
    })
  }catch(err){
    res.send({
      code:400,
      msg:'获取失败',
      data:err
    })
  }
})
router.put('/modifyFarmland',async (req,res) => {
  try{
    let { id,name,type,manager_id } = req.body
    let result = await farmlandDao.modifyFarmland(id,name,type,manager_id)
    res.send({
      code:200,
      msg:'获取成功',
      data:result
    })
  }catch(err){
    res.send({
      code:400,
      msg:'获取失败',
      data:err
    })
  }
})
router.get('/deleteFarmland',async (req,res) => {
  try{
    let id = req.query.id
    let result = await farmlandDao.deleteFarmland(id)
    res.send({
      code:200,
      msg:'获取成功',
      data:result
    })
  }catch(err){
    res.send({
      code:400,
      msg:'获取失败',
      data:err
    })
  }
})
module.exports = router